package com.dal.common.entity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 * @ClassName test
 * @Author jian
 * @Date 3:30 PM 2022/9/15
 **/
public class Test {
    public static String reflect(Class<?> obj, String content) {
        Field[] fields = obj.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            Class<?> type = fields[i].getType();
            String typeRes = "";
            if (type != null) {
                String tmp = type.getName();
                typeRes = tmp.substring(tmp.lastIndexOf('.')+1, tmp.length());
            }
            content += "|" + name + "|"+"是"+"|" + typeRes + "||\r\n";
            //System.out.print("|" + name + "|" + typeRes + "|||\r\n");
        }
        return content;
    }

    public static void saveAsFileWriter(String content, String classsName) {
        classsName = classsName.substring(classsName.lastIndexOf('.')+1, classsName.length());
        String savefile = "/Users/jian/Desktop/IO" + classsName + ".md";
        FileWriter fwriter = null;
        try {
            File file = new File(savefile);
            if(!file.exists()) {
                file.createNewFile();
            }
            fwriter = new FileWriter(savefile);
            fwriter.write(content);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        Class<DirectOrderDetail> obj = DirectOrderDetail.class;
        String content = "####数据库文档\r\n\r\n|参数名|必选|类型|说明|\r\n|-----|-----|-----|-----|\r\n";
        String res = reflect(obj, content);
        System.out.println(res);
        saveAsFileWriter(res,obj.getName());
    }
}
